package com.bankwel.j2d.model;

import com.bankwel.j2d.core.J2dMatrix;

/**
 * 
 * @author Acce1erator
 * @since 2015年12月28日 14:39:09
 */
public class Vector2d {

	private float x;
	private float y;

	/*******************************************/
	public Vector2d() {
		x = 0;
		y = 0;
	}

	public Vector2d(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public Vector2d(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public Vector2d(double x, double y) {
		this.x = (float) x;
		this.y = (float) y;
	}

	/*********************************************/
	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = y;
	}

	public void set(float x, float y) {
		this.x = x;
		this.y = y;
	}

	/*******************************************/

	public Vector2d transformBy(J2dMatrix j2d) {
		float[][] a = j2d.toArray();
		return new Vector2d(a[0][0] * x + a[0][1] * y + a[0][2], a[1][0] * x + a[1][1] * y + a[1][2]);
	}

	public Vector2d plus(float x, float y) {
		return new Vector2d(this.x + x, this.y + y);
	}

	public float norm() {
		return (float) Math.sqrt(x * x + y * y);
	}

	/*******************************************/
	public float[] toArray() {
		return new float[] { x, y };
	}

	@Override
	public String toString() {
		return new StringBuffer().append("<").append(x).append(",").append(y).append(">").toString();
	}
}
